package com.youkeyi.ddy.cloud.basic.crm.config.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.youkeyi.ddy.cloud.basic.crm.config.entity.SysCustomerInputConfig;
import com.youkeyi.ddy.cloud.basic.crm.config.service.SysCustomerInputConfigService;
import com.youkeyi.ddy.cloud.common.api.basic.crm.config.CrmCustomerInputConfigCloudService;
import com.youkeyi.ddy.cloud.common.api.basic.crm.config.dto.CrmCustomerInputConfigListCloudDto;
import com.youkeyi.ddy.cloud.common.api.basic.crm.config.dto.CrmCustomerInputConfigListItemCloudDto;
import com.youkeyi.ddy.cloud.common.api.basic.crm.config.param.CrmCustomerInputConfigCreateCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.crm.config.param.CrmCustomerInputConfigDeleteCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.crm.config.param.CrmCustomerInputConfigQueryCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.crm.config.param.CrmCustomerInputConfigUpdateCloudParam;
import com.youkeyi.ddy.cloud.common.constant.YoukeyiDdyCloudBasicPublicConstant;
import com.youkeyi.ddy.cloud.common.enums.ResponseCodeEnum;
import com.youkeyi.ddy.cloud.common.response.ResponseContent;
import com.youkeyi.ddy.cloud.common.utils.SimpleUUidUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * <p>
 * 客户信息录入配置 前端控制器
 * </p>
 *
 * @author xiawang.liu
 * @since 2024-03-01
 */
@Service
public class SysCustomerInputConfigController implements CrmCustomerInputConfigCloudService {

    @Autowired
    private SysCustomerInputConfigService sysCustomerInputConfigService;

    @Override
    public CrmCustomerInputConfigListCloudDto query(CrmCustomerInputConfigQueryCloudParam param) {
        CrmCustomerInputConfigListCloudDto dto = new CrmCustomerInputConfigListCloudDto();
        if (null == param) {
            dto.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return dto;
        }

        if (CollectionUtils.isNotEmpty(param.getCustomerInputConfigPrimaryKeys())) {
            param.getCustomerInputConfigPrimaryKeys().removeAll(Collections.singleton(null));
        }
        if (CollectionUtils.isNotEmpty(param.getCorpKeys())) {
            param.getCorpKeys().removeAll(Collections.singleton(null));
        }

        QueryWrapper<SysCustomerInputConfig> wrapper = new QueryWrapper<>();
        wrapper.lambda()
                .in(CollectionUtils.isNotEmpty(param.getCustomerInputConfigPrimaryKeys()), SysCustomerInputConfig::getPrimaryKey, param.getCustomerInputConfigPrimaryKeys())
                .in(CollectionUtils.isNotEmpty(param.getCorpKeys()), SysCustomerInputConfig::getCorpKey, param.getCorpKeys())
                .like(StringUtils.isNotBlank(param.getDisplayFieldsFuzzy()), SysCustomerInputConfig::getDisplayFields, param.getDisplayFieldsFuzzy())
                .ne(SysCustomerInputConfig::getIsDeleted, YoukeyiDdyCloudBasicPublicConstant.SYSTEM_YES_OR_NO_YES.value())
        ;

        Page<SysCustomerInputConfig> page = new Page<>();
        if (null != param.getCurrentPage() && null != param.getPageSize()) {
            page.setCurrent(param.getCurrentPage());
            page.setSize(param.getPageSize());
        } else {
            page.setCurrent(1);
            page.setSize(99999999);
        }
        Page<SysCustomerInputConfig> customerInputConfigList = sysCustomerInputConfigService.page(page, wrapper);
        if (CollectionUtils.isNotEmpty(customerInputConfigList.getRecords())) {
            dto.setTotal(customerInputConfigList.getTotal());

            List<CrmCustomerInputConfigListItemCloudDto> items = new ArrayList<>();
            customerInputConfigList.getRecords().forEach(customerInputConfig -> {
                CrmCustomerInputConfigListItemCloudDto item = new CrmCustomerInputConfigListItemCloudDto();
                item.setCustomerInputConfigPrimaryKey(customerInputConfig.getPrimaryKey());
                item.setCorpKey(customerInputConfig.getCorpKey());
                item.setDisplayFields(customerInputConfig.getDisplayFields());
                item.setIsDeleted(customerInputConfig.getIsDeleted());
                item.setIsDeletedName(YoukeyiDdyCloudBasicPublicConstant.SYSTEM_YES_OR_NO.getName(item.getIsDeleted()));
                item.setCreateTime(customerInputConfig.getCreateTime() != null ? customerInputConfig.getCreateTime().getTime() : null);
                item.setCreateUserKey(customerInputConfig.getCreateUserKey());
                item.setCreateUserName(customerInputConfig.getCreateUserName());
                item.setUpdateTime(customerInputConfig.getUpdateTime() != null ? customerInputConfig.getUpdateTime().getTime() : null);
                item.setUpdateUserKey(customerInputConfig.getUpdateUserKey());
                item.setUpdateUserName(customerInputConfig.getUpdateUserName());
                items.add(item);
            });
            dto.setRows(items);
        }
        dto.setSuccessMessage(ResponseCodeEnum.SUCCESS.getMessage());
        return dto;
    }

    @Override
    public ResponseContent create(CrmCustomerInputConfigCreateCloudParam param) {
        ResponseContent rc = new ResponseContent();
        if (null == param
                || StringUtils.isBlank(param.getCorpKey()) || StringUtils.isBlank(param.getDisplayFields())) {
            rc.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return rc;
        }
        SysCustomerInputConfig customerInputConfig = new SysCustomerInputConfig();
        customerInputConfig.setPrimaryKey(SimpleUUidUtils.getUuid());
        customerInputConfig.setCorpKey(param.getCorpKey());
        customerInputConfig.setDisplayFields(param.getDisplayFields());
        customerInputConfig.setIsDeleted(YoukeyiDdyCloudBasicPublicConstant.SYSTEM_YES_OR_NO_NO.value());
        customerInputConfig.setCreateUserKey(param.getCreateUserKey());
        customerInputConfig.setCreateUserName(param.getCreateUserName());
        customerInputConfig.setCreateTime(new Date());
        boolean save = sysCustomerInputConfigService.save(customerInputConfig);
        if (!save) {
            rc.setErrorMessage(ResponseCodeEnum.FAIL.getMessage());
            return rc;
        }
        rc.setSuccessMessage(ResponseCodeEnum.SUCCESS.getMessage());
        rc.setKey(customerInputConfig.getPrimaryKey());
        return rc;
    }

    @Override
    public ResponseContent update(CrmCustomerInputConfigUpdateCloudParam param) {
        ResponseContent rc = new ResponseContent();
        if (null == param || StringUtils.isBlank(param.getCustomerInputConfigPrimaryKey())) {
            rc.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return rc;
        }

        UpdateWrapper<SysCustomerInputConfig> wrapper = new UpdateWrapper<>();
        wrapper.lambda()
                .eq(SysCustomerInputConfig::getPrimaryKey, param.getCustomerInputConfigPrimaryKey());
        SysCustomerInputConfig customerInputConfig = new SysCustomerInputConfig();
        customerInputConfig.setPrimaryKey(param.getCustomerInputConfigPrimaryKey());
        customerInputConfig.setDisplayFields(param.getDisplayFields());
        customerInputConfig.setUpdateUserKey(param.getUpdateUserKey());
        customerInputConfig.setUpdateUserName(param.getUpdateUserName());
        customerInputConfig.setUpdateTime(new Date());
        boolean save = sysCustomerInputConfigService.update(customerInputConfig, wrapper);
        rc.setSuccessMessage(ResponseCodeEnum.SUCCESS.getMessage());
        if (!save) {
            rc.setErrorMessage(ResponseCodeEnum.FAIL.getMessage());
            return rc;
        }
        return rc;
    }

    @Override
    public ResponseContent delete(CrmCustomerInputConfigDeleteCloudParam param) {
        ResponseContent rc = new ResponseContent();
        if (null == param || CollectionUtils.isEmpty(param.getCustomerInputConfigPrimaryKeys())) {
            rc.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return rc;
        }
        UpdateWrapper<SysCustomerInputConfig> wrapper = new UpdateWrapper<>();
        wrapper.lambda()
                .in(SysCustomerInputConfig::getPrimaryKey, param.getCustomerInputConfigPrimaryKeys());

        SysCustomerInputConfig customerInputConfig = new SysCustomerInputConfig();
        customerInputConfig.setIsDeleted(YoukeyiDdyCloudBasicPublicConstant.SYSTEM_YES_OR_NO_YES.value());
        customerInputConfig.setUpdateUserKey(param.getUpdateUserKey());
        customerInputConfig.setUpdateUserName(param.getUpdateUserName());
        customerInputConfig.setUpdateTime(new Date());
        boolean save = sysCustomerInputConfigService.update(customerInputConfig, wrapper);
        rc.setSuccessMessage(ResponseCodeEnum.SUCCESS.getMessage());
        if (!save) {
            rc.setErrorMessage(ResponseCodeEnum.FAIL.getMessage());
        }
        return rc;
    }
}
